package com.ctgu.fcftsystem.controller;

import com.ctgu.fcftsystem.pojo.Spend;
import com.ctgu.fcftsystem.pojo.User;
import com.ctgu.fcftsystem.service.SpendService;
import com.ctgu.fcftsystem.service.UserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/SpendController")
public class SpendController {

    @Autowired
    private SpendService spendService;

    @Autowired
    private UserService userService;

    //分页
    @RequestMapping("/findByPaging")
    public Object findByPaging(int pageCount,int pageSize){
        ModelMap map = new ModelMap();
        PageHelper.startPage(pageCount, pageSize);//设置数据库分页查询的范围
        try {
            List<Spend> userList = spendService.findByPaging(pageCount, pageSize);
            //分页的信息
            PageInfo<Spend> pageInfo=new PageInfo<>(userList);
            map.put("msg","查询所有角色成功");
            map.put("data",pageInfo);
            map.put("code",0);
        }catch (Exception e){
            e.getStackTrace();
            map.put("msg","查询所有角色失败");
            map.put("code",1);
        }
        return map;
    }

    @RequestMapping("/addSpend")
    public Map addSpend(Integer snum, Integer stype, String swho, String spath, String sremark){
        Map<String,Object> map = new HashMap<>();
        if(snum!=null&&stype!=null&&swho!=null){
            Spend spend = new Spend();

            spend.setSpendNum(snum);
            spend.setSpendType(stype);
            spend.setSpendWho(swho);
            spend.setSpendDate(new Date());
            spend.setSpendPath(spath);
            spend.setSpendRemark(sremark);

            User beforeUser = userService.getUserMoneyById(swho);
            int beforeMoney = beforeUser.getUserMoney();
            int afterMoney = beforeMoney - snum;
            int res1 = userService.setUserMoneyById(swho,afterMoney);

            System.out.println(beforeMoney);
            System.out.println(afterMoney);
            System.out.println(res1);

            int res = spendService.addSpend(spend);
            if(res>0){
                //添加成功
                map.put("code",0);
                map.put("msg","添加成功");
                map.put("data",res);
            }else{
                //添加失败
                map.put("code",1);
                map.put("msg","添加失败");
            }
        }else{
            //信息不完整
            map.put("code",1);
            map.put("msg","请填写完整信息");
        }
        return map;
    }

    @RequestMapping("/getSpendList")
    public Map getSpendList(){
        Map<String,Object> map = new HashMap<>();
        List<Spend> list = spendService.getSpendList();
        if(list!=null&&!list.isEmpty()){
            //查询到所有支出记录
            map.put("code",0);
            map.put("msg","所有支出记录如下");
            map.put("data",list);
        }else{
            //未查到任何支出记录
            map.put("code",1);
            map.put("msg","没有任何支出记录");
        }
        return map;
    }

    @RequestMapping("/delSpendById")
    public Map delSpendById(int sid){
        Map<String,Object> map = new HashMap<>();
        int res = spendService.delSpendById(sid);
        if(res == 1){
            //删除成功
            map.put("code",0);
            map.put("msg","删除成功");
            map.put("data",res);
        }else{
            //删除失败
            map.put("code",1);
            map.put("msg","删除失败");
        }
        return map;
    }


    @RequestMapping("/updateSpend")
    public Map updateSpend(Integer sid,Integer snum, Integer stype, String swho, Date sdate,String spath, String sremark){
        Map<String,Object> map = new HashMap<>();
        Spend spend = new Spend();

        spend.setSpendId(sid!=null?sid:sid);
        spend.setSpendNum(snum!=null?snum:-1);
        spend.setSpendType(stype!=null?stype:-1);
        spend.setSpendWho(swho!=null?swho:swho);
        spend.setSpendDate(sdate!=null?sdate:sdate);
        spend.setSpendPath(spath!=null?spath:spath);
        spend.setSpendRemark(sremark!=null?sremark:sremark);

        int res = spendService.updateSpend(spend);
        if(res>0){
            //修改成功
            map.put("code",0);
            map.put("msg","修改成功");
            map.put("data",res);
        }else{
            //修改失败
            map.put("code",1);
            map.put("msg","修改失败");
        }
        return map;
    }

    @RequestMapping("/searchSpend")
    public Map searchSpend(Integer sid,Integer snum, Integer stype, String swho, Date sdate,String spath, String sremark){
        Map<String,Object> map = new HashMap<>();
        Spend spend = new Spend();

        spend.setSpendId(sid!=null?sid:-1);
        spend.setSpendNum(snum!=null?snum:-1);
        spend.setSpendType(stype!=null?stype:-1);
        spend.setSpendWho(swho!=null?swho:swho);
        spend.setSpendDate(sdate!=null?sdate:sdate);
        spend.setSpendPath(spath!=null?spath:spath);
        spend.setSpendRemark(sremark!=null?sremark:sremark);

        List<Spend> list = spendService.searchSpend(spend);
        if(list!=null&&!list.isEmpty()){
            //查询支出记录如下
            map.put("code",0);
            map.put("msg","查到支出记录如下");
            map.put("data",list);
        }else{
            //未查到任何支出记录
            map.put("code",1);
            map.put("msg","没用符合条件的支出记录");
        }
        return map;
    }
}
